﻿#include "mbasescene.h"
#include <QGraphicsView>
#include <QtMath>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>
#include <QTransform>
#include <QDebug>

int MBaseScene::gGridSize = 10;
int MBaseScene::gGridSquares = 5;
QColor MBaseScene::gBackgroundColor = QColor("#f2f4f6");
QColor MBaseScene::gForegroundColorDark = QColor("#ceefe4");
QColor MBaseScene::gForegroundColorLight = QColor("#f4f0e6");

bool MBaseScene::isbedrawLine = false;

MBaseScene::MBaseScene(QObject *parent)
    : QGraphicsScene(parent),
      bdrawLine(false)
{

     line = nullptr;

}

MBaseScene::~MBaseScene()
{

}

void MBaseScene::keyPressEvent(QKeyEvent *event)
{
    QGraphicsScene::keyPressEvent(event);
}


void MBaseScene::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Delete)
    {
        QList<QGraphicsItem *> list = this->selectedItems();
        foreach(QGraphicsItem *item , list)
        {
            this->removeItem(item);
        }
    }
    else{
       QGraphicsScene::keyReleaseEvent(event);
    }
}


void MBaseScene::EnableDrawLine(bool flag)
{
    bdrawLine = flag;
    isbedrawLine = flag;
    qDebug() << "bdrawLine:" << int(bdrawLine);
}


void MBaseScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if(bdrawLine){
        line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(),mouseEvent->scenePos()));
        QPen pen(Qt::black,1,Qt::DotLine);
        line->setPen(pen);
        this->addItem(line);
    }
    else
       QGraphicsScene::mousePressEvent(mouseEvent);
}

void MBaseScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if(bdrawLine && line != nullptr)
    {
        QLineF newLine(line->line().p1(),mouseEvent->scenePos());
        line->setLine(newLine);
    }else{
     QGraphicsScene::mouseMoveEvent(mouseEvent);
    }
}

void MBaseScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    if(bdrawLine && line != nullptr)
    {
        //创建一个item容器，调用items函数返回直线起始点的所有item项
        QList<QGraphicsItem*> StartItems = items(line->line().p1());
        //移除线项
        if(StartItems.count() && StartItems.first() == line)
            StartItems.removeFirst();

        //获取最上层的图元
        if(!StartItems.isEmpty()){
            QGraphicsItem *topItem = nullptr;
            for(QGraphicsItem *Item : StartItems){
                if(!topItem || Item->zValue() > topItem->zValue())
                {
                    topItem = Item;
                }
            }
            StartItems.clear();
            StartItems.append(topItem);
        }


        QList<QGraphicsItem *> EndItems = items(line->line().p2());
        // 移除线项
        if (EndItems.count() && EndItems.first() == line)
            EndItems.removeFirst();

        // 获取最上层的图元
        if (!EndItems.isEmpty()) {
            QGraphicsItem* topItem = nullptr;
            for (QGraphicsItem* item : EndItems) {
                if (!topItem || item->zValue() > topItem->zValue()) {
                    topItem = item;
                }
            }
            EndItems.clear();
            EndItems.append(topItem);
        }
        removeItem(line);
        delete line;
    /*还是要进行鼠标选择的比较*/
        if(StartItems.count() > 0 && EndItems.count() > 0
                && StartItems.first()->type() == PlugObject::Type
                && EndItems.first()->type() == SocketObject::Type
           /*    && StartItems.first() != EndItems.first(),可以用来判断是否为同一辆车上的插头*/)
        {
            PlugObject *StartItem = qgraphicsitem_cast<PlugObject*>(StartItems.first());
            SocketObject *EndItem = qgraphicsitem_cast<SocketObject*>(EndItems.first());


            //打印输出当前获取的类型后和在scene上的坐标
            if(StartItem)
            {
                qDebug() << "type :" << typeid (*StartItem).name();
                qDebug() << "position" << StartItem->scenePos();
                //测试相关值是否真的改变，是真的改变！
                qDebug() << StartItem->getcarNumber();
                qDebug() << StartItem->getPlugName();
            }
            if(EndItem)
            {
                qDebug() << "type :" <<typeid (*EndItem).name();
                qDebug() << "position" << EndItem->scenePos();
                qDebug() << EndItem->getcarNumber();
                qDebug() << EndItem->getSocketName();
            }


            //判断是否是同一辆车上的插头和插孔
            if(StartItem->getcarNumber() != EndItem->getcarNumber())
            {
            Line_Object *lineNew = new Line_Object(StartItem, EndItem);
            addItem(lineNew);
            //添加成功表示该箭头属性为连接
            lineNew->IsConnecting();
            }
            //lineNew->updatePosition();
            //这里更新有点多余，会自动更新
        }
        line = nullptr;
    }
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}


void MBaseScene::drawBackground(QPainter *painter, const QRectF &rect)
{
//    int MBaseView::gGridSize = 10;
//    int MBaseView::gGridSquares = 5;
//    QColor MBaseView::gBackgroundColor = QColor("#f2f4f6");
//    QColor MBaseView::gForegroundColorDark = QColor("#ceefe4");
//    QColor MBaseView::gForegroundColorLight = QColor("#f4f0e6");

    //修改背景色，进行不同颜色的转换，获取坐标
    int left = qFloor(rect.left()); /* 向下取整 */
    int right = qCeil(rect.right()); /* 向上取整 */
    int top = qFloor(rect.top());
    int bottom = qCeil(rect.bottom());

//将距离除以小格子边长，然后减去余数，作为左上开始点
    int _left = left - left % MBaseScene::gGridSize;
    int _top  = top  - top  %MBaseScene::gGridSize;

    QVector<QLine> light_lines, dark_lines;

    //计算一个大的方格占用的空间
    int wh =MBaseScene::gGridSize * MBaseScene::gGridSquares;

    for (int i = _left; i <= right; i += MBaseScene::gGridSize)
    {
        if(i % wh == 0)
            dark_lines.append(QLine(i, top, i, bottom));
        else
            light_lines.append(QLine(i, top, i, bottom));
    }

    for (int j = _top; j <= bottom; j +=MBaseScene::gGridSize) {
        if(j % wh == 0)
            dark_lines.append(QLine(left, j, right, j));
        else
            light_lines.append(QLine(left, j, right, j));
    }


//划线操作
    painter->setBrush(Qt::NoBrush);
    painter->setPen(MBaseScene::gForegroundColorLight);
    painter->drawLines(light_lines);
    painter->setPen(MBaseScene::gForegroundColorDark);
    painter->drawLines(dark_lines);
}


